//
//  HLAdViewController.m
//  HL-百思不得姐
//
//  Created by admin on 16/5/9.
//  Copyright © 2016年 CHL. All rights reserved.
//

#import "HLAdViewController.h"
#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import <UIImageView+WebCache.h>
#import "HLAdItem.h"
#import "HLTabBarController.h"

#define code2 @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"

@interface HLAdViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *launchImageView;

@property (weak, nonatomic) IBOutlet UIView *adContentView;

@property (strong,nonatomic) HLAdItem *adItem;

@property (strong,nonatomic) UIImageView *adImageView;

@property (weak, nonatomic) IBOutlet UIButton *jumpButton;

@property (weak,nonatomic) NSTimer *timer;

@end

@implementation HLAdViewController

- (IBAction)clickJumpBtn {
    
    UITabBarController *tabBarVC = [[HLTabBarController alloc] init];
    [UIApplication sharedApplication].keyWindow.rootViewController = tabBarVC;
    
    [self.timer invalidate];
}

#pragma MARK - 懒加载adImageView
- (UIImageView *)adImageView{
    if (_adImageView == nil) {
        UIImageView *imageView = [[UIImageView alloc] init];
        [self.adContentView addSubview:imageView];
        
        //点击手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
        [imageView addGestureRecognizer:tap];
        
        //UIImageView默认不允许与用户交互
        imageView.userInteractionEnabled = YES;
        
        _adImageView = imageView;
    }
    return _adImageView;
}

//点击事件
- (void)tap{
    NSURL *url = [NSURL URLWithString:self.adItem.ori_curl];
    UIApplication *app = [UIApplication sharedApplication];
    if ([app canOpenURL:url]) {
        [app openURL:url];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置启动图片
    [self setupLaunchImage];
    
    //加载广告数据
    [self loadAdView];
    
    //添加定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
}

#pragma mark - 定时器方法
- (void)timeChange{
    static int i = 3;
    if (i == 0) {
        [self clickJumpBtn];
    }
    i--;
    
    [self.jumpButton setTitle:[NSString stringWithFormat:@"跳转 (%d)",i] forState:UIControlStateNormal];
}

#pragma mark - 加载广告数据
-(void)loadAdView{
    //创建会话请求者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    //拼接参数
    NSMutableDictionary *parameter = [NSMutableDictionary dictionary];
    parameter[@"code2"] = code2;
    //请求数据
    // 请求数据 -> 解析数据(写成plist文件) -> 设计模型 -> 字典转模型 -> 展示数据
    [manager GET:@"http://mobads.baidu.com/cpro/ui/mads.php" parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //打印出需要的属性
        NSLog(@"%@",responseObject);
        
        //解析数据,将数据写成plis文件
        [responseObject writeToFile:@"/Applications/程序/大神班/项目/HL-百思不得姐/HL-百思不得姐/Classes/AD(广告)/ad.plist" atomically:YES];
        
        //获取字典,转换成模型
        NSDictionary *adDict = [responseObject[@"ad"] lastObject];
        self.adItem = [HLAdItem mj_objectWithKeyValues:adDict];
        
        //展示数据
        CGFloat h = HLScreenW / self.adItem.w * self.adItem.h;
        self.adImageView.frame = CGRectMake(0, 0, HLScreenW, h);
        [self.adImageView sd_setImageWithURL:[NSURL URLWithString:self.adItem.w_picurl]];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}



// 6p:LaunchImage-800-Portrait-736h@3x.png
// 6:LaunchImage-800-667h@2x.png
// 5:LaunchImage-568h@2x.png
// 4s:LaunchImage@2x.png
#pragma mark - 设置启动图片,屏幕适配
- (void)setupLaunchImage
{
    if (iphone6P) {
        self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-800-Portrait-736h@3x.png"];
    }else if (iphone6){
        self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-800-667h@2x.png"];
    }else if (iphone5){
        self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-568h@2x.png"];
    }else if (iphone4){
        self.launchImageView.image = [UIImage imageNamed:@"LaunchImage@2x.png"];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}


@end
